package com.jsyh.maidu.adapter.orders;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jsyh.maidu.R;
import com.jsyh.maidu.activity.me.LookOrderActivity;
import com.jsyh.maidu.model.ScanOrders;
import com.jsyh.maidu.presenter.DeleteScanOrderPresenter;

import java.util.List;

/**
 * Created by ZHY_9 on 2017/6/15.
 */

public class ScanOrderAdapter extends RecyclerView.Adapter<ScanOrderAdapter.ScanOrderHolder> {

    private Context context;
    private List<ScanOrders> datas;
    private DeleteScanOrderPresenter deleteScanOrderPresenter;

    public ScanOrderAdapter(Context context, List<ScanOrders> datas) {
        this.context = context;
        this.datas = datas;
    }

    @Override
    public ScanOrderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ScanOrderHolder holder = new ScanOrderHolder(LayoutInflater.from(context).inflate(R.layout.item_scan_orders, parent, false));
        return holder;
    }

    @Override
    public void onBindViewHolder(ScanOrderHolder holder, final int position) {
        holder.orderNumber.setText(Html.fromHtml("<font color='black'>订单编号：</font>"+"<font color='red'>"+ datas.get(position).getOrder_sn() +"</font>"));
        holder.storeName.setText("店铺名称：" + datas.get(position).getTitle());
        holder.orderTotal.setText("订单总额：￥" + datas.get(position).getAll_amount());
        holder.orderCoupon.setText("使用消费券：￥" + datas.get(position).getIntegral());
        holder.orderBalance.setText("使用余额：￥" + datas.get(position).getSurplus());
        holder.orderPayMoney.setText(Html.fromHtml("<font color='black'>支付金额：￥</font>"+"<font color='red'>"+ datas.get(position).getOrder_amount() +"</font>"));
        holder.orderTime.setText(datas.get(position).getAddtime());
        if (datas.get(position).getPay_status() == 2){
            holder.payed.setVisibility(View.VISIBLE);
            holder.cancel.setVisibility(View.GONE);
            holder.pay.setVisibility(View.GONE);
        }else {
            holder.payed.setVisibility(View.GONE);
            holder.cancel.setVisibility(View.VISIBLE);
            holder.pay.setVisibility(View.VISIBLE);
        }
        holder.cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                deleteScanOrderPresenter = new DeleteScanOrderPresenter();
                deleteScanOrderPresenter.deleteScanOrder(context, datas.get(position).getId());
                notifyItemRemoved(position);
                datas.remove(position);
            }
        });
        holder.pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, LookOrderActivity.class);
                intent.putExtra("orderid", String.valueOf(datas.get(position).getId()));
                context.startActivity(intent);
            }
        });
    }


    @Override
    public int getItemCount() {
        return datas.size();
    }

    class ScanOrderHolder extends RecyclerView.ViewHolder {

        TextView orderNumber;
        TextView storeName;
        TextView orderTotal;
        TextView orderCoupon;
        TextView orderBalance;
        TextView orderPayMoney;
        TextView orderTime;
        Button cancel;
        Button pay;
        Button payed;

        public ScanOrderHolder(View itemView) {
            super(itemView);
            orderNumber = (TextView) itemView.findViewById(R.id.order_number);
            storeName = (TextView) itemView.findViewById(R.id.store_name);
            orderTotal = (TextView) itemView.findViewById(R.id.order_total_money);
            orderCoupon = (TextView) itemView.findViewById(R.id.coupon_number);
            orderBalance = (TextView) itemView.findViewById(R.id.balance_number);
            orderPayMoney = (TextView) itemView.findViewById(R.id.pay_money);
            orderTime = (TextView) itemView.findViewById(R.id.order_time);
            cancel = (Button) itemView.findViewById(R.id.cancel_order);
            pay = (Button) itemView.findViewById(R.id.pay_button);
            payed = (Button) itemView.findViewById(R.id.payed_button);

        }
    }
}
